var users=new Array(); //存放所有用户的数组
var operateType; // 按钮的操作类型
// 创建用户的构造方法
function user(code,userName,sex,birthday,email,comments) {
    this.code=code;
    this.userName=userName;
    this.sex=sex;
    this.birthday=birthday;
    this.email=email;
    this.comments=comments;
}
// 初始化数据
function job() {
    users[0]=new user("1001","张三","女","1991-1-1","0000@qq,com","new student");
    users[1]=new user("1002","李四","男","1991-1-1","1111@qq.com","old student");
    users[2]=new user("1003","王五","女","1991-1-1","2222@qq.com","new student");
    users[3]=new user("1004","赵六","女","1991-1-1","3333@qq.com","new student");
    refresh(users);  
}

// 刷新表格
function refresh(users) {
    var tbody = document.getElementById("tbody");
    var html = "";
    var color = "warning";
    var flag = true;
    for (var i in users) {
        if (flag) {
            color = "info";
        } else {
            color = "warning";
        }
        html = html + "<tr class='" + color + "'><td style='width:30px;'><input type='checkbox'></td><td id='code'>"
            + users[i].code + "</td><td id='userName'>"
            + users[i].userName + "</td><td id='sex'>"
            + users[i].sex + "</td><td id='birthday'>"
            + users[i].birthday + "</td><td id='email'>"
            + users[i].email + "</td><td id='comments'>"
            + users[i].comments + "</td>"
            + "</tr>";
        flag = !flag;//颜色转换
    }
    tbody.innerHTML = html; // 在表格中创建所有tr元素
}

// 当点击添加按钮
$("#user_add").on("click", function () {
    operateType = "add";
});

//当点击修改按钮
$("#user_edit").on("click", function () {
    operateType = "edit";
});


//添加用户
$("#save").on("click", function () {
    if (operateType == "add") {
        var newUser = new user($("#m_code").val(), $("#m_userName").val(), $("#m_sex").val(), $("#m_birthday").val(), $("#m_email").val(), $("#m_comments").val());
        users = users.concat(newUser); 
    } else if (operateType == "edit") {
        for(var i = 0; i< users.length;i++){
            if(code == users[i].code){ 
                users[i].code =  $("#m_code").val();
                users[i].userName = $("#m_userName").val();
                users[i].sex =  $("#m_sex").val();
                users[i].birthday = $("#m_birthday").val();
                users[i].email = $("#m_email").val();
                users[i].comments = $("#m_comments").val();
            }
        }
        alert("修改成功");
    }
    refresh(users); 
});

//删除用户
$("#user_delete").on("click", function () {
    var checkboxs=$("input[type='checkbox']");  
    let f=false; 
    for (var i = 0; i < checkboxs.length; i++) {
        var box = checkboxs[i];  
        if (box.checked) {  
            f= true;  
            box=$(box); 
            let code=box.parent().siblings("#code").text(); 
            for(var i=0; i<users.length;i++){
                if(code == users[i].code){
                    users.splice(i,1); 
                    alert("删除成功");
                }
            }
        }
    }
    if(!f){
        alert("选择删除的用户");
    }
    refresh(users);
});
var code; 

//编辑用户
$("#user_edit").on("click", function () {
    var checkboxs=$("input[type='checkbox']");
    let f=false;
    for (var i=0; i<checkboxs.length;i++) {
        var box=checkboxs[i];
        if (box.checked) {
            f=true;
            $(this).attr("data-toggle","modal");  // 启用模态框
            $(this).attr("data-target","#myModal"); // 启用模态框
            box=$(box);
            code=box.parent().siblings("#code").text();
            // 获取表格中选中的用户的各项数据
            var userName=box.parent().siblings("#userName").text();
            var sex=box.parent().siblings("#sex").text();
            var birthday=box.parent().siblings("#birthday").text();
            var email=box.parent().siblings("#email").text();
            var comments=box.parent().siblings("#comments").text();
			// 选中用户的各项数据，放在模态框的相应input框中
            $("#m_code").val(code);
            $("#m_userName").val(userName);
            $("#m_sex").val(sex);
            $("#m_birthday").val(birthday);
            $("#m_email").val(email);
            $("#m_comments").val(comments);
        }
    }
    if(!f){
        alert("选择编辑的用户");
        $(this).attr("data-toggle",null); // 禁用模态框
        $(this).attr("data-target",null); // 禁用模态框
    }
});
var newusers;
// 查询用户
$("#user_find").on("click", function () {
    newusers = new Array();
    var codeLike = $(this).siblings("#s_code").val();
    for(var i = 0; i< users.length;i++){
        if(users[i].code.indexOf(codeLike)>=0){ 
            newusers = newusers.concat( users[i] );  
        }
    }
    refresh(newusers);
});
